package com.qf.main.pharmacy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.common.db.utils.CommonBeanutils;
import com.qf.main.pharmacy.common.qo.OrderMallQo;
import com.qf.main.pharmacy.common.qo.OrderQo;
import com.qf.main.pharmacy.common.vo.CartVo;
import com.qf.main.pharmacy.common.vo.PharmacyVO;
import com.qf.main.pharmacy.entity.Cart;
import com.qf.main.pharmacy.entity.Pharmacy;
import com.qf.main.pharmacy.mapper.CartMapper;
import com.qf.main.pharmacy.mapper.PharmacyMapper;
import com.qf.main.pharmacy.service.CartService;
import com.qf.main.pharmacy.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author chengqiang
 * @Create 2024-02-19-8:12
 * @Description:
 */
@Service
public class CartServiceImpl implements CartService {
    @Resource
    private CartMapper cartMapper;
    @Resource
    private PharmacyMapper pharmacyMapper;
    @Resource
    private OrderService orderService;


    @Override
    public CartVo getCart(int id) {
        QueryWrapper<Cart> qw = new QueryWrapper<>();
        qw.eq(Cart.COL_USER_ID,id);
        List<Cart> carts = cartMapper.selectList(qw);

        CartVo cartVo = CommonBeanutils.convertTo(carts.get(0), CartVo::new);
        LinkedList<PharmacyVO> pharmacyVOS = new LinkedList<>();
        for (Cart cart : carts) {
            QueryWrapper<Pharmacy> qw1 = new QueryWrapper<>();
            qw1.eq(Pharmacy.COL_PHARMACY_ID,cart.getMallId());
            Pharmacy pharmacy = pharmacyMapper.selectOne(qw1);
            PharmacyVO pharmacyVO = CommonBeanutils.convertTo(pharmacy, PharmacyVO::new);

            pharmacyVOS.add(pharmacyVO);
            cartVo.setMall(pharmacyVOS);
        }
        return cartVo;
    }

    @Override
    public Integer saveCart(Cart cart) {

        return cartMapper.insert(cart);

    }

    @Override
    public Integer removeCart(int userId,int mallId) {
        QueryWrapper<Cart> qw = new QueryWrapper<>();
        qw.eq(Cart.COL_MALL_ID,mallId);
        qw.eq(Cart.COL_USER_ID,userId);
        return cartMapper.delete(qw);
    }

    @Override
    @Transactional
    public long placeOrder(OrderQo orderQo) {

        orderQo.setOrderMallQoList(new ArrayList<>());

        QueryWrapper<Cart> qw = new QueryWrapper<>();
        qw.eq(Cart.COL_USER_ID,orderQo.getUserId());
        qw.eq(Cart.COL_MALL_STATUS,1);
        List<Cart> carts = cartMapper.selectList(qw);
        for (Cart cart : carts) {
            QueryWrapper<Pharmacy> qw1 = new QueryWrapper<>();
            qw1.eq(Pharmacy.COL_PHARMACY_ID,cart.getMallId());
            Pharmacy pharmacy = pharmacyMapper.selectOne(qw1);
            OrderMallQo orderMallQo = new OrderMallQo();
            orderMallQo.setOrderMallName(pharmacy.getPharmacyName());
            orderMallQo.setOrderMallPrice(pharmacy.getPharmacyPrice());
            orderMallQo.setOrderMallNumber(cart.getMallNumber());
            orderQo.getOrderMallQoList().add(orderMallQo);
        }
        return orderService.saveOrder(orderQo);
    }


}
